﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ChatIBOXClient
{
	/// <summary>
	/// Interaction logic for ChatWindow.xaml
	/// </summary>
	public partial class ChatWindow : Window
	{
        ServiceReference.Client receiver = new ChatIBOXClient.ServiceReference.Client ();
        ServiceReference.Client localClient = new ChatIBOXClient.ServiceReference.Client();
        ProxySingleton proxySingleton = ProxySingleton.GetInstance();
		public ChatWindow()
		{
			this.InitializeComponent();
            this.localClient = proxySingleton.localClient;
            proxySingleton.ProxyCallbackEvent += new ProxySingleton.ProxyCallbackEventHander(this.ProxySingleton_ProxyCallbackEvent);
            this.Closed += new EventHandler(windowClose_Click);
			// Insert code required on object creation below this point.
            this.textboxContent.Focus();
            
		}
        private void windowClose_Click(object sender, EventArgs e)
        {
            MainWindow.listChatting.Remove(receiver.Name);
        }
        #region Properties
        public ServiceReference.Client Receiver
        {
            get
            {
                return receiver;
            }
            set
            {
                receiver = value;
            }
        }
        #endregion

        #region Public Method
        
        #endregion

        #region Private Method
        public void ProxySingleton_ProxyCallbackEvent(object sender, ProxyCallbackEventArgs e)
        {
            switch (e.callbackType)
            {
                case CallbackType.Receive:
                    ReceiveMessage(e.message);
                    break;
                default:
                    break;
            }
        }

        public void ReceiveMessage(ServiceReference.Message message)
        {
            AppendText(message.Sender + " say:" +message.Content + Environment.NewLine);
            this.WindowState = WindowState.Normal;
            this.ShowInTaskbar = true;
        }

        private void AppendText(string text)
        {
            this.textboxHistory.AppendText(text);
        }

        private void buttonSend_Click(object sender, RoutedEventArgs e)
        {
            this.SendMessage();
            
        }
        #endregion

        private void textboxContent_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                this.SendMessage();
            }
        }

        private void SendMessage()
        {
            ServiceReference.Message message = new ChatIBOXClient.ServiceReference.Message();
            message.Content = textboxContent.Text;
            message.Sender = localClient.Name;
            AppendText(localClient.Name + " Say : " + this.textboxContent.Text + Environment.NewLine);
            textboxContent.Clear();
            this.textboxContent.Focus();
            proxySingleton.Whisper(message, this.receiver);
        }
        
	}
}